﻿using System;
using System.Web.Mvc;
using Microsoft.Ajax.Utilities;
using WebApplication4.Dao;
using Work3.Models;

namespace WebApplication4.Controllers
{
    public class adminController : Controller
    {
        private assetssDao _assetsDao = new assetssDao();
        public bool Auth() {
            if (Session["user"] == null) {
                Session["alert"] = "请先登录";
                return false;
            }
            return true;
        }
        public ActionResult Index() {
            if (!Auth()) {
                ViewBag.Status = 0;
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ViewBag.username = Session["user"];
                ViewBag.Status = 1;
            }
            if (Session["Insert"] == null)
            {
                ViewBag.Insert = 0;
            }
            if (Session["Alert"] != null) {
                ViewBag.Alert = Session["Alert"];
                Session.Remove("Alert");
            }
            if (Session["private"] == null)
            {
                ViewBag.assetss = _assetsDao.SelectAll();
            }
            else
            {
                ViewBag.assetss = _assetsDao.Select((string)Session["user"]);
            }

            if (Session["Show"] == null)
            {
                ViewBag.show = 0;
            }
            else
            {
                ViewBag.res = _assetsDao.showType();
                ViewBag.show = 1;
            }
            if (ViewBag.assetss != null)
            {
                ViewBag._max = getMax();
            }
            else
            {
                ViewBag._max = -1;
            }
            return View();
        }

        public ActionResult Searchassets(assets assets) {
            if (!Auth()) {
                return RedirectToAction("Index", "Home");
            }
            ViewBag.assetss = _assetsDao.Select(assets.Id);
            return View("Index");
        }

        public ActionResult Updateassets(assets assets) {
            if (!Auth()) {
                return RedirectToAction("Index", "Home");
            }
            Session["Alert"] = "修改" + (_assetsDao.Update(assets) ? "成功" : "失败");
            return RedirectToAction("Index");
        }
        
        public ActionResult Deleteassets(assets assets) {
            if (!Auth()) {
                return RedirectToAction("Index", "Home");
            }
            Session["Alert"] = "删除" + (_assetsDao.Delete(assets.Id) ? "成功" : "失败");
            return RedirectToAction("Index");
        }

        public ActionResult showMine(assets assets)
        {
            Session["private"] = 1;
            return RedirectToAction("Index");
        }

        public ActionResult showAll(assets assets)
        {
            Session.Remove("private");
            return RedirectToAction("Index");
        }
        
        public ActionResult insertNewAssets(assets assets)
        {
            ViewBag.Insert = 1;
            Session["Insert"] = 1;
            return RedirectToAction("Index");
        }

        public ActionResult addNew(assets assets)
        {
            ViewBag.Insert = 0;
            Session.Remove("Insert");
            _assetsDao.Insert(assets);
            return RedirectToAction("Index");
        }
        
        public ActionResult showNormal(assets assets)
        {
            Session.Remove("Show");
            return RedirectToAction("Index");
        }

        public int getMax()
        {
            int _max = 0;
            for (int i = 0; i < ViewBag.assetss.Count; i++)
            {
                if (_max < ViewBag.assetss[i].Id)
                {
                    _max = ViewBag.assetss[i].Id;
                }
            }
            return _max;
        }
        
        public ActionResult showDetail(assets assets)
        {
            
            Session["Show"] = 1;
            return RedirectToAction("Index");
        }
    }
}